/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.interpreter.frontends.functional;

import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import xowl.lang.owl2.Annotation;
import xowl.lang.owl2.Axiom;
import xowl.lang.owl2.IRI;
import xowl.lang.owl2.Ontology;

/**
 *
 * @author Laurent WOUTERS
 */
public class FunctionalWriter implements xowl.interpreter.frontends.Writer {
    private AxiomWriter axiomWriter;
    private Map<String, String> mapPrefixes;
    private String currentIRI;
    
    public String toShortIRI(IRI expression) {
        String[] parts = expression.getHasValue().split("#");
        String ontology = parts[0];
        String name = parts[1];
        if (currentIRI.equals(ontology)) {
            return (":" + name);
        } else {
            parts = ontology.split("/");
            String prefix = parts[parts.length-1];
            String tested = prefix;
            Integer i = 0;
            while (mapPrefixes.containsKey(tested) && !mapPrefixes.get(tested).equals(ontology)) {
                i++;
                tested = prefix + i.toString();
            }
            if (!mapPrefixes.containsKey(tested))
                mapPrefixes.put(tested, ontology);
            return tested + ":" + name;
        }
    }
    
    @Override public void write(Writer writer, Ontology ontology, Collection<Axiom> axioms) throws IOException {
        currentIRI = ontology.getHasIRI().getHasValue();
        mapPrefixes = new HashMap<String, String>();
        axiomWriter = new AxiomWriter(this);

        java.io.CharArrayWriter subWriter = new java.io.CharArrayWriter();
        for (Annotation annotation : ontology.getAllAnnotations()) {
            subWriter.write("\t");
            axiomWriter.writeAnnotation(subWriter, annotation);
            subWriter.write("\n");
        }
        for (Axiom axiom : axioms) {
            subWriter.write("\t");
            axiomWriter.writeAxiom(subWriter, axiom);
            subWriter.write("\n");
        }
        
        writer.write("Prefix(rdf:=<" + xowl.interpreter.frontends.VocRDF.rdf + ">)\n");
        writer.write("Prefix(rdfs:=<" + xowl.interpreter.frontends.VocRDF.rdfs + ">)\n");
        writer.write("Prefix(xsd:=<" + xowl.interpreter.frontends.VocRDF.xsd + ">)\n");
        writer.write("Prefix(owl:=<" + xowl.interpreter.frontends.VocRDF.owl + ">)\n");
        for (String prefix : mapPrefixes.keySet())
            writer.write("Prefix("+prefix+":=<" + mapPrefixes.get(prefix) + "#>)\n");
        writer.write("Prefix(:=<" + currentIRI + "#>)\n");
        writer.write("\n");
        writer.write("Ontology(<" + currentIRI + ">\n");
        writer.write(subWriter.toCharArray());
        writer.write(")\n");
    }
}
